import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;


public class LevelEditor {

	ArrayList<ArrayList<Element>> level = new ArrayList<ArrayList<Element>>();
	ArrayList<Element> lands = new ArrayList<Element>();
	ArrayList<Element> objects = new ArrayList<Element>();
	ArrayList<Element> enemies = new ArrayList<Element>();
	
	ArrayList<Element> levelLand = new ArrayList<Element>();
	ArrayList<Element> levelObjects = new ArrayList<Element>();
	ArrayList<Element> levelEnemies = new ArrayList<Element>();
	
	private boolean landMenu;
	private boolean objectMenu;
	private boolean enemyMenu;
	private boolean distanceMenu;
	private boolean selected = false;
	
	
	private Element temp;
	
	int selectedI;
	int selectedA;
	int yDif;
	int xDif;
	
	
	public LevelEditor(Applet myApplet)
	{
		landMenu = false;
		objectMenu = false;
		enemyMenu = false;
		
		//Establish Menus' Graphics Positions
		for(int i = 0; i < 7; i++)
		{
			if (i == 0)
				temp = new Element(584,542,144,72,"grassXXL", myApplet.getImage(myApplet.getCodeBase(), "grassXXL.png"));

			if (i == 1)
				temp = new Element(274,542,144,33,"grassXL", myApplet.getImage(myApplet.getCodeBase(), "grassXL.png"));
			
			if (i == 2) 
				temp = new Element(445,318,66,144,"grassWall", myApplet.getImage(myApplet.getCodeBase(), "grassWall.png"));

			if (i == 3) 
				temp = new Element(272,238,96,45,"grassLg", myApplet.getImage(myApplet.getCodeBase(), "grassLg.png"));

			if (i == 4) 
				temp = new Element(421,238,72,33,"grassMed", myApplet.getImage(myApplet.getCodeBase(), "grassMed.png"));
			
			if(i == 5)
				temp = new Element(580,238,48,33,"grassSmall", myApplet.getImage(myApplet.getCodeBase(), "grassSmall.png"));
			
			if(i == 6)
				temp = new Element(580,400,150,4,"deathBarrierH", myApplet.getImage(myApplet.getCodeBase(), "deathBarrierH.png"));
			lands.add(new Element(temp));
		}
		
		for(int i = 0; i < 5; i++)
		{
			if (i == 0)
				temp = new Element(584,542,16,14,"money", myApplet.getImage(myApplet.getCodeBase(), "treasure.gif"));

			if (i == 1)
				temp = new Element(274,542,16,14,"health", myApplet.getImage(myApplet.getCodeBase(), "health.png"));
			
			if (i == 2) 
				temp = new Element(445,318,16,24,"finishDoor", myApplet.getImage(myApplet.getCodeBase(), "finishDoor.png"));

			if (i == 3) 
				temp = new Element(272,238,16,24,"startDoor", myApplet.getImage(myApplet.getCodeBase(), "startDoor.png"));

			if (i == 4) 
				temp = new Element(421,238,30,29,"tree", myApplet.getImage(myApplet.getCodeBase(), "tree.png"));
			
			
			objects.add(new Element(temp));
		}
		
		for(int i = 0; i < 7; i++)
		{
			if(i == 0)
				temp = new Element(584,410,23,40,"0", myApplet.getImage(myApplet.getCodeBase(), "metal1.png"));
			if(i == 1)
				temp = new Element(274,542,34,33,"1", myApplet.getImage(myApplet.getCodeBase(), "chompx1.png"));
			if(i == 2)
				temp = new Element(445,318,22,10,"2", myApplet.getImage(myApplet.getCodeBase(), "sentry1.png"));
			if(i == 3)
				temp = new Element(272,238,49,74,"3", myApplet.getImage(myApplet.getCodeBase(), "chompB0.png"));
			if(i == 4)
				temp = new Element(475,318,24,13,"4", myApplet.getImage(myApplet.getCodeBase(), "sentry2.png"));
			if(i == 5)
				temp = new Element(475,500,206,127,"5", myApplet.getImage(myApplet.getCodeBase(), "hellSpawnR0.png"));
			if(i == 6)
				temp = new Element(272,400,36,52,"6", myApplet.getImage(myApplet.getCodeBase(), "stingerTurret.png"));
			
			enemies.add(new Element(temp));
		}
		
		
		
		level.add(levelLand);
		level.add(levelObjects);
		level.add(levelEnemies);
		
	}
	
	public void act(int x, int y, boolean terrainButton, boolean objectButton, boolean enemyButton, boolean escape, boolean enter, boolean delete, boolean shift, boolean pressed)
	{
		
		if(landMenu || objectMenu || enemyMenu)
		{
			if(landMenu)
				for(int i = 0; i < lands.size(); i++)
				{
					if(lands.get(i).getRectangle().contains(x,y))
					{
							level.get(0).add(new Element(lands.get(i)));
							landMenu = false;
					}
				}
			
			if(objectMenu)
				for(int i = 0; i < objects.size(); i++)
				{
					if(objects.get(i).getRectangle().contains(x,y))
					{
							level.get(1).add(new Element(objects.get(i)));
							objectMenu = false;
					}
				}
			
			if(enemyMenu)
				for(int i = 0; i < enemies.size(); i++)
				{
					if(enemies.get(i).getRectangle().contains(x,y))
					{
							level.get(2).add(new Element(enemies.get(i)));
							enemyMenu = false;
					}
				}
			
			if(escape)
			{
				landMenu = false;
				objectMenu = false;
				enemyMenu = false;
				distanceMenu = false;
			}
				
		}
		else
		{
			if(!landMenu && !objectMenu && !enemyMenu)
			{
				if(!pressed)
				{
					if(terrainButton && !objectButton && !enemyButton)
						landMenu = true;
					else if(!terrainButton && objectButton && !enemyButton)
							objectMenu = true;
					else if(!terrainButton && !objectButton && enemyButton)
							enemyMenu = true;
					
					selected = false;
					distanceMenu = false;
					selectedI = 0;
					selectedA = 0;
					xDif = 0;
					yDif = 0;
				}
				
				if(pressed)
				{
					if(!selected)
					for(int i = 0; i < level.size(); i++)
					{
							for(int a = 0; a < level.get(i).size(); a++)
							{
								if(level.get(i).get(a).getRectangle().contains(x,y))
									{
										selectedI = i;
										selectedA = a;
										xDif = x-level.get(i).get(a).getRectangle().x;
										yDif = y-level.get(i).get(a).getRectangle().y;
										selected = true;
										
										if(shift)
										{
											distanceMenu = true;
											level.get(i).get(a).setInitialPos(x, y);
										}
											
									}
							}
					}
					
					//Move Object
					if(!level.get(selectedI).isEmpty() && selected && !distanceMenu)
						level.get(selectedI).get(selectedA).getRectangle().setLocation(x-xDif,y-yDif);
					
					//Delete Object
					if(!level.get(selectedI).isEmpty() && selected && delete)
					{
						level.get(selectedI).remove(selectedA);
						selected = false;
					}
					
					//Set Distance for moving objects
					if(!level.get(selectedI).isEmpty() && selected && distanceMenu)
					{
						level.get(selectedI).get(selectedA).setMovement(x, y);
					}
				}
			}
		}
		
	}
	
	public void draw(Graphics g, Applet myApplet)
	{
		g.setColor(Color.BLACK);
		g.clearRect(0, 0, 1000, 860);
		if(!landMenu && !objectMenu && !enemyMenu)
		{
			for(int i = 0; i < level.size(); i++)
			{
					for(int a = 0; a < level.get(i).size(); a++)
					{
							level.get(i).get(a).draw(g, myApplet);
					}
			}
		}
		else
		{

			g.drawRect(250, 215, 500, 413);
			
			if(landMenu)
				for(int i = 0; i < lands.size(); i++)
				{
					lands.get(i).draw(g, myApplet);
				}	
			
			if(objectMenu)
				for(int i = 0; i < objects.size(); i++)
				{
					objects.get(i).draw(g, myApplet);
				}
			
			if(enemyMenu)
				for(int i = 0; i < enemies.size(); i++)
				{
					enemies.get(i).draw(g, myApplet);
				}
			g.setColor(Color.WHITE);
		}
	}
	
	
	public void compileLevel()
	{
			for(int i = 0; i < level.size(); i++)
			{
				String n = "";
				String pack = "";
				int var = 0;
				int enemyYMod = 0;
					for(int a = 0; a < level.get(i).size(); a++)
					{
						if(i == 0)
						{
							n = "Terrain";
							pack = "terrainPack";
						}
						if(i == 1)
						{
							n = "Object";
							if(level.get(i).get(a).getType().equalsIgnoreCase("startDoor") || level.get(i).get(a).getType().equalsIgnoreCase("finishDoor") || level.get(i).get(a).getType().equalsIgnoreCase("tree"))
							{
								pack = level.get(i).get(a).getType();
							}
							else
								{
									pack = "pickups";
									var = 10;
								}
							
						}
						if(i == 2)
						{
							n = "Enemy";
							pack = "enem";
							if(level.get(i).get(a).getType().equalsIgnoreCase("0"))
								var = 0;
							
							if(level.get(i).get(a).getType().equalsIgnoreCase("1"))
							{
								var = 1;
								enemyYMod = 24;
							}
							if(level.get(i).get(a).getType().equalsIgnoreCase("2"))
							{
								var = 2;
								enemyYMod = 10;
							}
							if(level.get(i).get(a).getType().equalsIgnoreCase("3"))
							{
								var = 3;
								enemyYMod = 39;
							}
							if(level.get(i).get(a).getType().equalsIgnoreCase("4"))
							{
								var = 4;
								enemyYMod = 13;
							}
							if(level.get(i).get(a).getType().equalsIgnoreCase("5"))
							{
								var = 5;
								enemyYMod = 7;
							}
							if(level.get(i).get(a).getType().equalsIgnoreCase("6"))
							{
								var = 6;
							}
						}
						
						if(pack.equalsIgnoreCase("terrainPack"))
							System.out.println(pack + ".add(new " + n + "(" + level.get(i).get(a).getRectangle().x + "," + level.get(i).get(a).getRectangle().y + "," + "\"" + level.get(i).get(a).getType()+ "\"" + level.get(i).get(a).compileMovement() + ", myApplet));");
						else if(!pack.contains("Door"))
							{
							if(!pack.equalsIgnoreCase("enem"))
								System.out.println(pack + ".add(new " + n + "(" + level.get(i).get(a).getRectangle().x + "," + level.get(i).get(a).getRectangle().y + "," + var + "," + "\"" + level.get(i).get(a).getType()+ "\"" + ", myApplet));");
							else System.out.println(pack + ".add(new " + n + "(" + level.get(i).get(a).getRectangle().x + "," + (level.get(i).get(a).getRectangle().y + enemyYMod) + "," + var + ", myApplet));");
							}
							else System.out.println(pack + " = " + "new " + n + "(" + level.get(i).get(a).getRectangle().x + "," + level.get(i).get(a).getRectangle().y + "," + var + "," + "\"" + level.get(i).get(a).getType()+ "\"" + ", myApplet);");
					
						if(i == level.size()-1 && a == level.get(level.size()-1).size()-1)
							System.out.println("=================================================================");
					}
			}
	}
	
	
}
